import SSE from "sse.js";

function createSSE(onMessage, onError = R.identity) {
  return connect(onMessage, onError);
}

const connect = R.curryN(_connect, 4);

function _connect(url, headers, onMessage, onError) {
  const sse = new SSE(url, { headers });
  sse.addEventListener("message", onMessage);
  sse.addEventListener("error", onError);
}

_connect.prototype.close = function () {
  this.sse.close();
};

export default connect;
